<template>
  <div class="login-container">
    <div class="login-placeholder"></div>
    <div class="login-form-wrapper">
      <el-form
        class="login-form"
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        autocomplete="on"
        label-position="left">
        <div class="title-container">
          <h3 class="title">{{ title }}</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <i class="ri-user-3-line"></i>
          </span>
          <el-input
            ref="usernameRef"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on" />
        </el-form-item>

        <el-tooltip v-model="capslockTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <i class="ri-lock-line"></i>
            </span>
            <el-input
              :key="passwordType"
              ref="passwordRef"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup="checkCapsLock"
              @blur="capslockTooltip = false"
              @keyup.enter="handleLogin" />
            <span class="show-pwd" @click="changePasswordType">
              <i :class="passwordType === 'password' ? 'ri-eye-close-line' : 'ri-eye-line'"></i>
            </span>
          </el-form-item>
        </el-tooltip>

        <el-checkbox class="remember-me" v-model="isRememberMe">一周内免登录</el-checkbox>

        <router-link to="/register">
          <span class="forget-password">忘记密码?</span>
        </router-link>

        <el-button :loading="loading" type="primary" style="width: 100%" @click.prevent="handleLogin">Login</el-button>

        <router-link to="/register">
          <span class="register">注册</span>
        </router-link>

        <!--      <el-button type="success" style="width:100%" @click="showDialog = true">-->
        <!--        Or connect with-->
        <!--      </el-button>-->
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
  import { computed, defineComponent, onMounted, ref } from 'vue';
  import { useLoginForm } from './hooks/useLoginForm';
  import { useQuery } from './hooks/useQuery';

  import { useStore } from 'vuex';
  import { useCapsLock } from '@/views/login/hooks/useCapsLock';
  import { usePasswordType } from '@/views/login/hooks/usePasswordType';
  import { useLoading } from '@/hooks/useLoading';
  import { LocationQueryRaw, useRouter } from 'vue-router';
  import { ElMessage } from 'element-plus';
  import { ELForm, ELInput } from '@/utils/types';

  export default defineComponent({
    name: 'Login',
    setup: function () {
      const router = useRouter();
      const store = useStore();
      const { loginForm, loginRules } = useLoginForm();
      const { redirect, otherQuery } = useQuery();
      const title = computed(() => store.getters['settings/title']);
      const { capslockTooltip, checkCapsLock } = useCapsLock();
      const { passwordType, changePasswordType } = usePasswordType();
      const { loading, setLoading } = useLoading();

      const loginFormRef = ref(null as ELForm | null);
      const usernameRef = ref(null as ELInput | null);
      const passwordRef = ref(null as ELInput | null);

      const isRememberMe = ref(false);

      onMounted(() => {
        if (loginForm.username === '') {
          usernameRef.value?.focus();
        } else {
          passwordRef.value?.focus();
        }
      });

      const handleLogin = async () => {
        loginFormRef.value?.validate(async valid => {
          if (valid) {
            setLoading();
            try {
              const info = await store.dispatch('account/login', {
                type: 1,
                principal: loginForm.username,
                credential: loginForm.password,
                rememberMe: isRememberMe.value,
              });
              console.log(info);
              ElMessage.success('登录成功!');
              await router.push({
                path: redirect.value,
                query: otherQuery.value as unknown as LocationQueryRaw,
              });
            } catch (ignored) {
              ElMessage.error('登录失败!');
            } finally {
              setLoading();
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      };

      return {
        title,
        loginForm,
        loginRules,
        isRememberMe,
        loginFormRef,
        usernameRef,
        passwordRef,
        redirect,
        otherQuery,
        capslockTooltip,
        checkCapsLock,
        passwordType,
        changePasswordType,
        loading,
        setLoading,
        handleLogin,
        showDialog: false,
      };
    },
  });
</script>

<style lang="scss">
  $bg: #f6f4fc;

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      .el-input__wrapper {
        background: transparent;
        border: none;
        box-shadow: none;
        margin-left: 10px;
        padding: 12px 5px 12px 15px;
        color: $black;
        height: 47px;
        width: 100%;
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #f6f4fc;
  $dark_gray: #889aa4;

  @import '~@/assets/styles/index.scss';

  .login-container {
    background: url(~@/assets/image/login_background.jpg) center center fixed no-repeat;
    background-size: 100% 100%;
    position: fixed;
    height: 100%;
    width: 100%;
    display: flex;

    .login-placeholder {
      color: transparent;
      flex-basis: 50%;
    }

    .login-form-wrapper {
      @extend .login-placeholder;
      display: flex;
      justify-content: center;
      align-items: center;

      .login-form {
        flex-basis: 65%;
        min-width: 300px;
        max-width: 500px;

        .el-form-item {
          background-color: #f6f4fc;
        }

        .remember-me {
          font-size: 14px;
          font-weight: 500;
          float: left;
          margin-bottom: 10px;
        }

        .text {
          font-size: 14px;
          font-weight: 500;
          color: $text;

          &:hover {
            color: $blue;
            cursor: pointer;
          }
        }

        .forget-password {
          @extend .text;
          float: right;
          margin-bottom: 10px;
        }

        .register {
          @extend .forget-password;
          margin-top: 10px;
        }
      }

      .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
          &:first-of-type {
            margin-right: 16px;
          }
        }
      }

      .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
      }

      .title-container {
        position: relative;

        .title {
          font-size: 26px;
          color: $black;
          margin: 0 auto 40px auto;
          text-align: center;
          font-weight: bold;
        }
      }

      .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
      }

      .el-button + .el-button {
        margin-left: 0;
      }
    }

    /*.third-party-button {*/
    /*	margin-top: 20px;*/
    /*	position: absolute;*/
    /*	right: 0;*/
    /*	bottom: 6px;*/
    /*}*/

    /*@media only screen and (max-width: 470px) {*/
    /*	.third-party-button {*/
    /*		display: none;*/
    /*	}*/
    /*}*/
  }
</style>
